///////////////////////////////////////////////////////////////////////////////////////////////////
// Illusion Ray Development Kit (IDK)           Copyright (c) 2010 ( dimitriy.martynenko@gmail.com )
///////////////////////////////////////////////////////////////////////////////////////////////////
// Created : 2010-05-22
// Updated : ...
// Licence : NEW BSD
// Info	   : IDK Engine
///////////////////////////////////////////////////////////////////////////////////////////////////
#pragma once

#include <IDK.hpp>
#include <RenderState.hpp>

namespace Renderer
{
	class TAbstractContext : public TObject 
	{
	public:
		virtual void SwapBuffers() = 0;
	};

	typedef ref<TAbstractContext> AbstractContext;


	class TRenderer : public Threading::TNonThread, virtual public IDK::IRenderer
	{
		typedef Threading::TNonThread Base;
		IDKClass;
	protected:
		typedef Array<ref<IDK::IRenderer::IPass>> Passes;
		Passes mPasses;

		AbstractContext mContext;
		IDK::Viewport mViewport;
		RenderState mRenderState;
		bool mActive;
		bool mNeedReset;
		bool mLockReset;
	protected://is Abstract
		TRenderer();
	protected:
		virtual RenderState CreateRenderState() = 0;
		virtual AbstractContext CreateContext() = 0;
	private:
		void CheckResetDevice(void);
	public:/*IModule*/
		virtual void OnInit(void);
		virtual void OnStart(void);
		virtual void OnFinish(void);
		virtual void OnUpdate(float dt);
		virtual void OnActive(bool active);
		virtual void Lock(bool reset);
		virtual void Unlock(void);
	public:
		void BeginRender();
		void EndRender();
		void Swap(float dt);
	public:///IRenderContext
		virtual void SetViewport(ref<IDK::IViewport> viewport);
		virtual ref<IDK::IViewport> GetViewport(void);
		virtual void AddPass(ref<IDK::IRenderer::IPass> pass);
		virtual void ClearPasses(void);
	public:
		virtual void OnThreadStart(void);
		virtual void OnThreadStop(void);
		virtual bool OnThreadUpdate(float dt, bool ready);
		virtual void OnThreadLock(void);
	};

	typedef ref<TRenderer> RendererGL;
}